package mn.idax.exchange.otc.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import mn.idax.exchange.common.CalendarUtil;
import mn.idax.exchange.otc.common.mq.sqs.SqsUtil;
import mn.idax.exchange.otc.constant.CommonConstant;
import mn.idax.exchange.otc.domain.EmaiDto.BaseEmaiDTO;
import mn.idax.exchange.otc.domain.EmaiDto.DownOrderDTO;
import mn.idax.exchange.otc.domain.EmaiDto.MessageBodyDTO;
import mn.idax.exchange.otc.domain.EmaiDto.ReleaseConinDTO;
import mn.idax.exchange.otc.entity.Emailnotifymsg;
import mn.idax.exchange.otc.mapper.EmailnotifymsgMapper;
import mn.idax.exchange.otc.service.SqsService;
import mn.idax.exchange.otc.util.LogUtils;
import mn.idax.exchange.otc.util.MessageNotifyUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class SqsServiceImpl implements SqsService {

    @Autowired
    EmailnotifymsgMapper emailnotifymsgMapper;

    @Override
    @Transactional
    public void sendEmailMessage(Integer emailType, String email, String lang, Integer relatebusinessid, String queueName, BaseEmaiDTO baseEmaiDTO) {
        try {
            Emailnotifymsg emailnotifymsg = new Emailnotifymsg();
            emailnotifymsg.setEmailtype(emailType);
            emailnotifymsg.setEmail(email);
            emailnotifymsg.setLang(lang);
            emailnotifymsg.setRelatebusinessid(relatebusinessid);
            emailnotifymsg.setIssend(false);
            emailnotifymsg.setIp("0.0.0.0");
            emailnotifymsg.setCreatetime(CalendarUtil.getUTCDate().getTime());
            emailnotifymsg.setUpdatetime(CalendarUtil.getUTCDate().getTime());
            emailnotifymsgMapper.insert(emailnotifymsg);
            //发送sqs
            Map msgBody = Maps.newHashMap();
            msgBody.put("Type", CommonConstant.SqsType.smsAndEmail );
            MessageBodyDTO messageBodyDTO = new MessageBodyDTO();
            messageBodyDTO.setId(emailnotifymsg.getId());
            messageBodyDTO.setType(emailType);
            messageBodyDTO.setRemark(JSON.toJSONString(baseEmaiDTO));
            msgBody.put("MessageBody", JSON.toJSONString(messageBodyDTO));
            SqsUtil.sendMessage(queueName, JSON.toJSONString(msgBody));
            LogUtils.info(SqsServiceImpl.class,"sendEmailMessage msgBody is:"+JSON.toJSONString(msgBody));
        }catch (Exception e){
            LogUtils.info(SqsServiceImpl.class,"sendEmailMessage invoke error,"+ExceptionUtils.getStackTrace(e));
        }

    }

}
